﻿using Md.Api.Model;
using Md.Api.Model.Im.Request;
using Md.Api.Service.Im;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Md.Api.Controllers;

/// <summary>
/// im
/// </summary>
[ApiController]
[Route("md-api/im")]
public class ImController : ControllerBase
{
	public string Ip => this.Request.Headers["X-Real-IP"].FirstOrDefault() ?? this.Request.HttpContext.Connection.RemoteIpAddress.ToString();

    /// <summary>
    /// Im连接服务
    /// </summary>
    public ImConnectService imConnectService { get; set; }
    /// <summary>
    /// Im连接
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>ResponseMessageModel</returns>
    [Route("connect"), HttpPost, AllowAnonymous]
	public async Task<ResponseMessageModel> ImConnect([FromBody] RequestImConnectModel model)
	{
        return await Task.Run(() => imConnectService.Execute(model));
	}

    /// <summary>
    /// im加入频道服务
    /// </summary>
    public ImJoinChannelService imJoinChannelService { get; set; }
    /// <summary>
    /// im加入频道
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>ResponseMessageModel</returns>
    [Route("join/channel"), HttpPost, AllowAnonymous]
    public async Task<ResponseMessageModel> ImJoinChannel([FromBody] RequestImJoinChannelModel model)
    {
        return await Task.Run(() => imJoinChannelService.Execute(model));
    }

    /// <summary>
    /// im发送消息服务
    /// </summary>
    public ImSendService imSendService { get; set; }
    /// <summary>
    /// im发送消息
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>ResponseMessageModel</returns>
    [Route("send"), HttpPost, AllowAnonymous]
    public async Task<ResponseMessageModel> ImSend([FromBody] RequestImSendModel model)
    {
        return await Task.Run(() => imSendService.Execute(model));
    }
}

